package com.wanglei.ssm.dao;

import com.wanglei.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户数据操作实体
 *
 * @author wl
 */
public interface UserDao {

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return 用户信息
     * @throws Exception e
     */
    @Select("select * from users where id = #{id}")
    @Results(value = {
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "email", column = "email"),
        @Result(property = "password", column = "password"),
        @Result(property = "phoneNum", column = "phoneNum"),
        @Result(property = "status", column = "status"),
        @Result(property = "roles", column = "id", many = @Many(select = "com.wanglei.ssm.dao.RoleDao.findRoleByUserId"))
    })
    UserInfo findById(String id) throws Exception;

    /**
     * 根据用户名查询用户信息，角色信息，权限信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Select("select * from users where username = #{username}")
    @Results({
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "email", column = "email"),
        @Result(property = "password", column = "password"),
        @Result(property = "phoneNum", column = "phoneNum"),
        @Result(property = "status", column = "status"),
        @Result(property = "roles", column = "id", many = @Many(select = "com.wanglei.ssm.dao.RoleDao.findRoleByUserId"))
    })
    UserInfo findByUsername(String username);

    /**
     * 查询所有用户信息
     *
     * @return 用户信息集合
     * @throws Exception e
     */
    @Select("select * from users")
    List<UserInfo> queryAll() throws Exception;

    /**
     * 用户保存方法
     *
     * @param user 用户信息
     * @throws Exception e
     */
    @Insert("insert into users(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void insertUser(UserInfo user) throws Exception;

    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
    void addRoleToUser(@Param("userId") String userId, @Param("roleId") String roleId) throws Exception;
}
